import Event from "../../GameFramework/Base/Event";
import { EPokerStatus, ESuit } from "../ConfigESuit";
import UIPoker from "../View/UIPoker/UIPoker";
import GameEvent from "./GameEvents";

export class Poker {
  public point: number = -1; //点数
  public suit: ESuit = ESuit.HEITAO; //花色
  private _status: EPokerStatus = EPokerStatus.CLOSE; //花色
  public get status(): EPokerStatus {
    return this._status;
  }
  public set status(status: EPokerStatus) {
    this._status = status;
  }
  private _view: UIPoker = null;

  constructor(point: number, suit: ESuit, status: EPokerStatus) {
    this.point = point;
    this.suit = suit;
    this.status = status;
  }

  public get view(): UIPoker {
    return this._view;
  }

  public Bind(view: UIPoker) {
    this._view = view;
  }
  public unBind() {
    this._view = null;
  }
}
export class PokerGroup {
  AddPoker(poker: Poker) {
    this._pokers.push(poker);
  }
  private _pokers: Poker[] = [];
  public get pokers(): Poker[] {
    return this._pokers;
  }
}

/**
 * 牌局数据
 */
export default class GameDB extends Event {

  /*************************************
   * property
   *************************************/
  //所有扑克牌原始数据
  private _pokers: Poker[] = [];
  //发牌区盖着的扑克
  private _closeAreaPokers: Poker[] = [];
  //发牌区掀开着的扑克
  private _openAreaPokers: Poker[] = [];
  //收牌区组
  private _reserveAreaPockerGroups: PokerGroup[] = [];
  //玩牌区组
  private _playAreaPockerGroups: PokerGroup[] = [];

  /*************************************
   * private api
   *************************************/
  constructor() {
    super();
  }
  /*************************************
   * static api
   *************************************/
  public static readonly CONST_RECEIVE_GROUPS: number = 4;
  public static readonly CONST_PLAY_GROUPS: number = 7;
  public Init() {
    // let gameDB = new GameDB();
    // return gameDB;
    //初始化牌局结构
    for (let i = 0; i < GameDB.CONST_RECEIVE_GROUPS; i++) {
      let pokerGroup = new PokerGroup();
      this._reserveAreaPockerGroups.push(pokerGroup);
    }
    for (let i = 0; i < GameDB.CONST_PLAY_GROUPS; i++) {
      let pokerGroup = new PokerGroup();
      this._playAreaPockerGroups.push(pokerGroup);
    }
    //初始化扑克数据 (1-13)*4花色
    for (let point = 1; point <= 13; point++) {
      for (let suit = 0; suit < 4; suit++) {
        let poker = new Poker(point, suit, EPokerStatus.CLOSE);
        this._pokers.push(poker);
      }
    }

    //派发初始化牌局事件
    this.emit(GameEvent.INIT_POKER, this._pokers);
  }

  /*************************************
   * getter & setter
   *************************************/
  public get pokers(): Poker[] {
    return this._pokers;
  }
  public get closeAreaPokers(): Poker[] {
    return this._closeAreaPokers;
  }
  public get openAreaPokers(): Poker[] {
    return this._openAreaPokers;
  }
  public get reserveAreaPockerGroups(): PokerGroup[] {
    return this._reserveAreaPockerGroups;
  }
  public get playAreaPockerGroups(): PokerGroup[] {
    return this._playAreaPockerGroups;
  }
  private shuff(pokers: Poker[], count: number = 300) {
    for (let i = 0; i < count; i++) {
      let si = parseInt('' + Math.random() * pokers.length, 10)
      let ei = parseInt('' + Math.random() * pokers.length, 10)
      let tmp = pokers[si]
      pokers[si] = pokers[ei]
      pokers[ei] = tmp
    }
  }
  /*************************************
   * public api
   *************************************/
  public Play() {
    //洗牌
    this.shuff(this._pokers)

    // console.log("pokre",this._pokers);

    //将牌放到发牌区
    let tmp = this._closeAreaPokers;
    this._closeAreaPokers = this._pokers;
    this._pokers = tmp;
    //通知UI层,数据发生变化
    this.emit(GameEvent.PLAY, this._pokers);

    //发牌
    for (let cards = GameDB.CONST_PLAY_GROUPS; cards >= 1; cards--) {
      // console.log("发牌1::cards",cards);

      for (let i = 0; i < cards; i++) {
        // console.log("i::",i);

        let cardGroupIndex = GameDB.CONST_PLAY_GROUPS - cards + i;
        let cardGroup: PokerGroup = this._playAreaPockerGroups[cardGroupIndex];
        let poker = this._closeAreaPokers[this._closeAreaPokers.length - 1];
        this._closeAreaPokers.length = this._closeAreaPokers.length - 1;

        poker.status = i === 0 ? EPokerStatus.OPEN : EPokerStatus.CLOSE;
        cardGroup.AddPoker(poker);
        // console.log("发牌2::cardGroup", cardGroup);

        this.emit(GameEvent.INIT_GROUP_CARD, cardGroupIndex, GameDB.CONST_PLAY_GROUPS - cards, poker);
      }
    }
  }
  public OnEventPokerMoveFromPlayToRecive(poker: Poker) {
    console.log("OnEventPokerMoveFromPlayToRecive", poker);
  }
  public isLocationPlayArea(poker: Poker): boolean {

    return this.playAreaPockerGroups.filter(
      pg => pg.pokers.filter(p => p.point === poker.point && p.suit === poker.suit).length > 0
    ).length > 0
  }
  public isIndexPlayAreaGroupTop(poker: Poker): boolean {
    for (const pg of this.playAreaPockerGroups) {
      let pokers = pg.pokers
      if (pokers.length > 0) {
        let p = pokers[pokers.length - 1]
        if (p.point == poker.point && p.suit == poker.suit) {
          return true
        }
      }
    }
    return false
  }
}
